package com.zy.ivideo.service;

import com.zy.ivideo.entity.PlayEntity;
import com.zy.ivideo.entity.ResponseEntity;
import com.zy.ivideo.repository.PlayRepository;
import com.zy.ivideo.repository.VideoMultitypeRepository;
import com.zy.ivideo.repository.VideoSimpleRepository;
import com.zy.ivideo.utils.ResponseUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class PlayService {
    @Autowired
    PlayRepository repository;

    @Autowired
    VideoSimpleRepository videoSimpleRepository;

    @Autowired
    VideoMultitypeRepository videoMultitypeRepository;

    /**
     * 同步播放次数
     * @param entity
     * @return
     */
    public ResponseEntity<PlayEntity> syncPlayNums(PlayEntity entity){
        PlayEntity result = repository.save(entity);
        if (result!=null){
            int r=0;
            if (entity.getSourceflag()==0){
                r=videoSimpleRepository.updatePlayNum(entity.getItemcode());
            }else if (entity.getSourceflag()==1){
                r=videoMultitypeRepository.updatePlayNum(entity.getItemcode());
            }
            if (r>0){
                return ResponseUtils.success(result);
            }
            return ResponseUtils.failed("同步播放次数失败");
        }
        return ResponseUtils.failed("更新播放次数失败");
    }
}
